
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第一步 · test search plus plugin</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Running-a-workflow.html" />
    
    
    <link rel="prev" href="Introduction-to-Webpack.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="Introduction-to-Webpack.html">
            
                <a href="Introduction-to-Webpack.html">
            
                    
                    介绍 Webpack
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.2.1" data-path="Getting-started.html">
            
                <a href="Getting-started.html">
            
                    
                    第一步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="Running-a-workflow.html">
            
                <a href="Running-a-workflow.html">
            
                    
                    开始工作流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="Automatic-browser-refresh.html">
            
                <a href="Automatic-browser-refresh.html">
            
                    
                    浏览器自动刷新
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="Requiring-files.html">
            
                <a href="Requiring-files.html">
            
                    
                    引入文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Introduction-to-React-JS.html">
            
                <a href="Introduction-to-React-JS.html">
            
                    
                    React JS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="Configuring-react-js.html">
            
                <a href="Configuring-react-js.html">
            
                    
                    配置 React JS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="Optimizing-rebundling.html">
            
                <a href="Optimizing-rebundling.html">
            
                    
                    优化重合并
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="Type-checking-with-flow.html">
            
                <a href="Type-checking-with-flow.html">
            
                    
                    Flow
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="CSS-Fonts-Images.md">
            
                <span>
            
                    
                    CSS, Fonts and Images
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="Loading-CSS.html">
            
                <a href="Loading-CSS.html">
            
                    
                    加载 CSS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="Automatic-CSS-refresh.html">
            
                <a href="Automatic-CSS-refresh.html">
            
                    
                    自动刷新 CSS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="Loading-LESS-or-SASS.html">
            
                <a href="Loading-LESS-or-SASS.html">
            
                    
                    加载 LESS 和 SASS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="Inlining-images.html">
            
                <a href="Inlining-images.html">
            
                    
                    内联 images
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="Inlining-fonts.html">
            
                <a href="Inlining-fonts.html">
            
                    
                    内联 fonts
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="Deployment-strategies.md">
            
                <span>
            
                    
                    部署策略
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="Structuring-configuration.html">
            
                <a href="Structuring-configuration.html">
            
                    
                    发布配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="Single-bundle.html">
            
                <a href="Single-bundle.html">
            
                    
                    合并成单文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="Split-app-and-vendors.html">
            
                <a href="Split-app-and-vendors.html">
            
                    
                    分离应用和第三方
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="Multiple-entry-points.html">
            
                <a href="Multiple-entry-points.html">
            
                    
                    多重入口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="Lazy-loaded-entry-points.html">
            
                <a href="Lazy-loaded-entry-points.html">
            
                    
                    懒加载入口文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="Isomorphic-app.html">
            
                <a href="Isomorphic-app.html">
            
                    
                    同构渲染
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="Wing-It-Like-a-Pro.html">
            
                <a href="Wing-It-Like-a-Pro.html">
            
                    
                    进阶
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="Optimizing-development.html">
            
                <a href="Optimizing-development.html">
            
                    
                    优化开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="Hot-loading-components.html">
            
                <a href="Hot-loading-components.html">
            
                    
                    热加载组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="JavaScript-next.html">
            
                <a href="JavaScript-next.html">
            
                    
                    使用下一代 JavaScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="Optimizing-caching.html">
            
                <a href="Optimizing-caching.html">
            
                    
                    优化缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="Matchers.html">
            
                <a href="Matchers.html">
            
                    
                    匹配器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="Lazy-loading-entry-points.md">
            
                <span>
            
                    
                    懒加载入口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="Creating-a-common-bundle.md">
            
                <span>
            
                    
                    创建一个复用代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="Understanding-chunks.html">
            
                <a href="Understanding-chunks.html">
            
                    
                    理解 Chunks
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="Authoring-libraries.html">
            
                <a href="Authoring-libraries.html">
            
                    
                    创建库
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >第一步</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <blockquote>
<p>Before getting started you should make sure you have a recent version of Node.js and NPM installed. See <a href="http://nodejs.org/" target="_blank">nodejs.org</a> for installation details. We&apos;ll use NPM to set up various tools.</p>
<p>&#x5728;&#x5F00;&#x59CB;&#x4E4B;&#x524D;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x628A;&#x4F60;&#x7684; Node.js &#x548C; NPM &#x90FD;&#x66F4;&#x65B0;&#x5230;&#x6700;&#x65B0;&#x7684;&#x7248;&#x672C;&#x3002;&#x8BBF;&#x95EE; <a href="http://nodejs.org/" target="_blank">nodejs.org</a> &#x67E5;&#x770B;&#x5B89;&#x88C5;&#x8BE6;&#x60C5;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x4F1A;&#x4F7F;&#x7528; NPM &#x5B89;&#x88C5;&#x4E00;&#x4E9B;&#x5DE5;&#x5177;&#x3002;</p>
</blockquote>
<p>Getting started with Webpack is straightforward. I&apos;ll show you how to set up a simple project based on it. As a first step, set a directory for your project and hit <code>npm init</code> and fill in some answers. That will create a <code>package.json</code> for you. Don&apos;t worry if some fields don&apos;t look ok, you can modify those later.</p>
<p>&#x5F00;&#x59CB;&#x4F7F;&#x7528; Webpack &#x975E;&#x5E38;&#x7B80;&#x5355;&#xFF0C;&#x6211;&#x4F1A;&#x5C55;&#x793A;&#x7ED9;&#x4F60;&#x770B;&#x4F7F;&#x7528;&#x5B83;&#x7684;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x9879;&#x76EE;&#x3002;&#x7B2C;&#x4E00;&#x6B65;&#xFF0C;&#x4E3A;&#x4F60;&#x7684;&#x9879;&#x76EE;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x5939;&#xFF0C;&#x7136;&#x540E;&#x8F93;&#x5165; <code>npm init</code>&#xFF0C;&#x7136;&#x540E;&#x586B;&#x5199;&#x76F8;&#x5173;&#x95EE;&#x9898;&#x3002;&#x8FD9;&#x6837;&#x4F1A;&#x4E3A;&#x4F60;&#x521B;&#x5EFA;&#x4E86; <code>package.json</code>&#xFF0C;&#x4E0D;&#x7528;&#x62C5;&#x5FC3;&#x586B;&#x9519;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4E4B;&#x540E;&#x4FEE;&#x6539;&#x5B83;&#x3002;</p>
<h2 id="&#x5B89;&#x88C5;-webpack">&#x5B89;&#x88C5; Webpack</h2>
<p>Next you should get Webpack installed. We&apos;ll do a local install and save it as a project dependency. This way you can invoke the build anywhere (build server, whatnot). Run <code>npm i webpack --save-dev</code>. If you want to run the tool, hit <code>node_modules/.bin/webpack</code>.</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x5B89;&#x88C5; Webpack&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x628A;&#x5B83;&#x5B89;&#x88C5;&#x5728;&#x672C;&#x5730;&#xFF0C;&#x7136;&#x540E;&#x628A;&#x5B83;&#x4F5C;&#x4E3A;&#x9879;&#x76EE;&#x4F9D;&#x8D56;&#x4FDD;&#x5B58;&#x4E0B;&#x6765;&#x3002;&#x8FD9;&#x6837;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x4F55;&#x5730;&#x65B9;&#x7F16;&#x8BD1;&#xFF08;&#x670D;&#x52A1;&#x7AEF;&#x7F16;&#x8BD1;&#x4E4B;&#x7C7B;&#x7684;&#xFF09;&#x3002;&#x8F93;&#x5165; <code>npm i webpack --save-dev</code>&#x3002;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x8FD0;&#x884C;&#x5B83;&#xFF0C;&#x5C31;&#x8F93;&#x5165; <code>node_modules/.bin/webpack</code>&#x3002;</p>
<h2 id="&#x76EE;&#x5F55;&#x7ED3;&#x6784;">&#x76EE;&#x5F55;&#x7ED3;&#x6784;</h2>
<p>Structure your project like this:</p>
<p>&#x9879;&#x76EE;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x957F;&#x8FD9;&#x6837;&#xFF1A;</p>
<ul>
<li>/app<ul>
<li>main.js</li>
<li>component.js</li>
</ul>
</li>
<li>/build<ul>
<li>bundle.js (&#x81EA;&#x52A8;&#x521B;&#x5EFA;)</li>
<li>index.html</li>
</ul>
</li>
<li>package.json</li>
<li>webpack.config.js</li>
</ul>
<p>In this case we&apos;ll create <code>bundle.js</code> using Webpack based on our <code>/app</code>. To make this possible, let&apos;s set up <code>webpack.config.js</code>.</p>
<p>&#x6211;&#x4EEC;&#x4F1A;&#x4F7F;&#x7528; Webpack &#x5728;&#x6211;&#x4EEC;&#x7684; <code>/app</code> &#x91CC;&#x6765;&#x81EA;&#x52A8;&#x521B;&#x5EFA; <code>bundle.js</code> &#x3002;&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x8BBE;&#x7F6E; <code>webpack.config.js</code>&#x3002;</p>
<h2 id="&#x8BBE;&#x7F6E;-webpack">&#x8BBE;&#x7F6E; Webpack</h2>
<p>Webpack &#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x957F;&#x8FD9;&#x6837;&#xFF1A;</p>
<p><em>webpack.config.js</em></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);


<span class="hljs-built_in">module</span>.exports = {
    entry: path.resolve(__dirname, <span class="hljs-string">&apos;app/main.js&apos;</span>),
    output: {
        path: path.resolve(__dirname, <span class="hljs-string">&apos;build&apos;</span>),
        filename: <span class="hljs-string">&apos;bundle.js&apos;</span>,
    },
};
</code></pre>
<h2 id="&#x8FD0;&#x884C;&#x4F60;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x7F16;&#x8BD1;">&#x8FD0;&#x884C;&#x4F60;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x7F16;&#x8BD1;</h2>
<p>Now that we have basic configuration in place, we&apos;ll need something to build. Let&apos;s start with a classic <code>Hello World</code> type of app. Set up <code>/app</code> like this:</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6709;&#x4E86;&#x4E00;&#x4E2A;&#x6700;&#x7B80;&#x5355;&#x7684;&#x914D;&#x7F6E;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x6709;&#x4EC0;&#x4E48;&#x4E1C;&#x897F;&#x53BB;&#x7F16;&#x8BD1;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x5F00;&#x59CB;&#x4E00;&#x4E2A;&#x7ECF;&#x5178;&#x7684; <code>Hello World</code>&#xFF0C;&#x8BBE;&#x7F6E; <code>/app</code> &#x50CF;&#x8FD9;&#x6837;&#xFF1A;</p>
<p><em>app/component.js</em></p>
<pre><code class="lang-javascript"><span class="hljs-meta">&apos;use strict&apos;</span>;


<span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> element = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">&apos;h1&apos;</span>);

    element.innerHTML = <span class="hljs-string">&apos;Hello world&apos;</span>;

    <span class="hljs-keyword">return</span> element;
};
</code></pre>
<p><em>app/main.js</em></p>
<pre><code class="lang-javascript"><span class="hljs-meta">&apos;use strict&apos;</span>;
<span class="hljs-keyword">var</span> component = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./component.js&apos;</span>);


<span class="hljs-built_in">document</span>.body.appendChild(component());
</code></pre>
<p>Now run <code>webpack</code> in your terminal and your application will be built. A <em>bundle.js</em> file will appear in your <code>/build</code> folder. Your <em>index.html</em> file in the <code>build/</code> folder will need to load up the application.</p>
<p>&#x73B0;&#x5728;&#x5728;&#x4F60;&#x7684;&#x547D;&#x4EE4;&#x884C;&#x8FD0;&#x884C; <code>webpack</code>&#xFF0C;&#x7136;&#x540E;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x4F1A;&#x5F00;&#x59CB;&#x7F16;&#x8BD1;&#xFF0C;&#x4E00;&#x4E2A; <code>bundle.js</code> &#x6587;&#x4EF6;&#x5C31;&#x8FD9;&#x6837;&#x51FA;&#x73B0;&#x5728;&#x4F60;&#x7684; <code>/build</code> &#x6587;&#x4EF6;&#x5939;&#x4E0B;&#xFF0C;&#x9700;&#x8981;&#x5728; <code>build/</code> &#x4E0B;&#x7684; <em>index.html</em> &#x53BB;&#x542F;&#x52A8;&#x9879;&#x76EE;&#x3002;</p>
<p><em>build/index.html</em></p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>/&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;bundle.js&quot;</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<blockquote>
<p>It would be possible to generate this file with Webpack using <a href="https://www.npmjs.com/package/html-webpack-plugin" target="_blank">html-webpack-plugin</a>. You can give it a go if you are feeling adventurous. It is mostly a matter of configuration. Generally this is the way you work with Webpack.</p>
<p>&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x53EF;&#x4EE5;&#x7528; <a href="https://www.npmjs.com/package/html-webpack-plugin" target="_blank">html-webpack-plugin</a> &#x6765;&#x751F;&#x6210;&#x3002;&#x5982;&#x679C;&#x4F60;&#x89C9;&#x5F97;&#x5192;&#x9669;&#xFF0C;&#x90A3;&#x5C31;&#x628A;&#x5269;&#x4E0B;&#x7684;&#x5DE5;&#x5177;&#x4EA4;&#x7ED9;&#x5B83;&#x6765;&#x505A;&#x3002;&#x4F7F;&#x7528;&#x5B83;&#x5C31;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x914D;&#x7F6E;&#x7684;&#x95EE;&#x9898;&#x3002;&#x4E00;&#x822C;&#x6765;&#x8BF4;&#x4F7F;&#x7528; Webpack &#x6765;&#x5DE5;&#x4F5C;&#x5C31;&#x662F;&#x8FD9;&#x4E48;&#x4E2A;&#x5957;&#x8DEF;&#x3002;</p>
</blockquote>
<h2 id="&#x8FD0;&#x884C;&#x5E94;&#x7528;">&#x8FD0;&#x884C;&#x5E94;&#x7528;</h2>
<p>Just double-click the <em>index.html</em> file or set up a web server pointing to the <code>build/</code> folder.</p>
<p>&#x53EA;&#x8981;&#x53CC;&#x51FB; <em>index.html</em> &#x6216;&#x8005;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A; Web &#x670D;&#x52A1;&#x6307;&#x5411; <code>build/</code> &#x6587;&#x4EF6;&#x5939;&#x3002;</p>
<h2 id="&#x8BBE;&#x7F6E;-packagejson-scripts">&#x8BBE;&#x7F6E; <code>package.json</code> <em>scripts</em></h2>
<p>It can be useful to run build, serve and such commands through <code>npm</code>. That way you don&apos;t have to worry about the technology used in the project. You just invoke the commands. This can be achieved easily by setting up a <code>scripts</code> section to <code>package.json</code>.</p>
<p><code>npm</code> &#x662F;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x597D;&#x7528;&#x7684;&#x7528;&#x6765;&#x7F16;&#x8BD1;&#x7684;&#x6307;&#x4EE4;&#xFF0C;&#x901A;&#x8FC7; <code>npm</code> &#x4F60;&#x53EF;&#x4EE5;&#x4E0D;&#x7528;&#x53BB;&#x62C5;&#x5FC3;&#x9879;&#x76EE;&#x4E2D;&#x4F7F;&#x7528;&#x4E86;&#x4EC0;&#x4E48;&#x6280;&#x672F;&#xFF0C;&#x4F60;&#x53EA;&#x8981;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x6307;&#x4EE4;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#xFF0C;&#x53EA;&#x8981;&#x4F60;&#x5728; <code>package.json</code> &#x4E2D;&#x8BBE;&#x7F6E; <code>scripts</code> &#x7684;&#x503C;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#x3002;</p>
<p>In this case we can move the build step behind <code>npm run build</code> like this:</p>
<p>&#x5728;&#x8FD9;&#x4E2A;&#x6848;&#x4F8B;&#x4E2D;&#x6211;&#x4EEC;&#x628A;&#x7F16;&#x8BD1;&#x6B65;&#x9AA4;&#x653E;&#x5230; <code>npm run build</code> &#x4E2D;&#x662F;&#x8FD9;&#x6837;&#xFF1A;</p>
<ol>
<li><code>npm i webpack --save</code> - If you want to install Webpack just a development dependency, you can use <code>--save-dev</code>. This is handy if you are developing a library and don&apos;t want it to depend on the tool (bad idea!).</li>
<li>Add the following to <code>package.json</code>:</li>
</ol>
<ol>
<li><code>npm i webpack --save</code> - &#x5982;&#x679C;&#x4F60;&#x53EA;&#x662F;&#x60F3;&#x8981;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x5F00;&#x53D1;&#x4F9D;&#x8D56;&#x5B89;&#x88C5; Webpack&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>--save-dev</code> &#x53C2;&#x6570;&#x3002;&#x5F53;&#x4F60;&#x5F00;&#x53D1;&#x7684;&#x9879;&#x76EE;&#x4E2D;&#x4E0D;&#x4F9D;&#x8D56;&#x8FD9;&#x4E2A;&#x5DE5;&#x5177;&#x7684;&#x65F6;&#x5019;&#x8FD9;&#x4E2A;&#x547D;&#x4EE4;&#x662F;&#x975E;&#x5E38;&#x4FBF;&#x5229;&#x7684;&#x3002;&#xFF08;&#x4E0D;&#x662F;&#x4E2A;&#x597D;&#x4E3B;&#x610F;&#xFF01;&#xFF09;</li>
<li>&#x628A;&#x4E0B;&#x9762;&#x7684;&#x5185;&#x5BB9;&#x6DFB;&#x52A0;&#x5230; <code>package.json</code>&#x4E2D;&#x3002;</li>
</ol>
<pre><code class="lang-json">  <span class="hljs-string">&quot;scripts&quot;</span>: {
    <span class="hljs-string">&quot;build&quot;</span>: <span class="hljs-string">&quot;webpack&quot;</span>
  }
</code></pre>
<p>To invoke a build, you can hit <code>npm run build</code> now.</p>
<p>&#x73B0;&#x5728;&#x4F60;&#x53EF;&#x4EE5;&#x8F93;&#x5165; <code>npm run build</code> &#x5C31;&#x53EF;&#x4EE5;&#x7F16;&#x8BD1;&#x4E86;&#x3002;</p>
<p>Later on this approach will become more powerful as project complexity grows. You can hide the complexity within <code>scripts</code> while keeping the interface simple.</p>
<p>&#x5F53;&#x9879;&#x76EE;&#x8D8A;&#x53D1;&#x590D;&#x6742;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x65B9;&#x6CD5;&#x4F1A;&#x53D8;&#x5F97;&#x8D8A;&#x6765;&#x8D8A;&#x6709;&#x6548;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x628A;&#x6240;&#x6709;&#x590D;&#x6742;&#x7684;&#x64CD;&#x4F5C;&#x9690;&#x85CF;&#x5728; <code>scripts</code> &#x91CC;&#x9762;&#x6765;&#x4FDD;&#x8BC1;&#x754C;&#x9762;&#x7684;&#x7B80;&#x6D01;&#x3002;</p>
<p>The potential problem with this approach is that it can tie you to a Unix environment in case you use environment specific commands. If so, you may want to consider using something environment agnostic, such as <a href="https://www.npmjs.com/package/gulp-webpack" target="_blank">gulp-webpack</a>.</p>
<p>&#x4E0D;&#x8FC7;&#x6F5C;&#x5728;&#x7684;&#x95EE;&#x9898;&#x662F;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x4F1A;&#x5BFC;&#x81F4;&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;&#x4E00;&#x4E9B;&#x7279;&#x6B8A;&#x7684;&#x6307;&#x4EE4;&#x7684;&#x65F6;&#x5019;&#x53EA;&#x80FD;&#x5728; Unix &#x73AF;&#x5883;&#x4E2D;&#x4F7F;&#x7528;&#x3002;&#x6240;&#x4EE5;&#x5982;&#x679C;&#x4F60;&#x9700;&#x8981;&#x8003;&#x8651;&#x4E00;&#x4E9B;&#x672A;&#x77E5;&#x7684;&#x73AF;&#x5883;&#x4E2D;&#x7684;&#x8BDD;&#xFF0C;&#x90A3;&#x4E48; <a href="https://www.npmjs.com/package/gulp-webpack" target="_blank">gulp-webpack</a> &#x4F1A;&#x662F;&#x4E00;&#x4E2A;&#x597D;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x3002;</p>
<blockquote>
<p>Note that NPM will find Webpack. <code>npm run</code> adds it to the <code>PATH</code> temporarily so our simple incantation will work.</p>
<p>&#x6CE8;&#x610F; NPM &#x4F1A;&#x627E;&#x5230; Webpack&#xFF0C;<code>npm run</code> &#x4F1A;&#x628A;&#x4ED6;&#x4E34;&#x65F6;&#x52A0;&#x5230; <code>PATH</code>&#x6765;&#x8BA9;&#x6211;&#x4EEC;&#x8FD9;&#x4E2A;&#x795E;&#x5947;&#x7684;&#x547D;&#x4EE4;&#x5DE5;&#x4F5C;&#x3002;</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Introduction-to-Webpack.html" class="navigation navigation-prev " aria-label="Previous page: 介绍 Webpack">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Running-a-workflow.html" class="navigation navigation-next " aria-label="Next page: 开始工作流">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第一步","level":"1.2.1","depth":2,"next":{"title":"开始工作流","level":"1.2.2","depth":2,"path":"Running-a-workflow.md","ref":"Running-a-workflow.md","articles":[]},"previous":{"title":"介绍 Webpack","level":"1.2","depth":1,"path":"Introduction-to-Webpack.md","ref":"Introduction-to-Webpack.md","articles":[{"title":"第一步","level":"1.2.1","depth":2,"path":"Getting-started.md","ref":"Getting-started.md","articles":[]},{"title":"开始工作流","level":"1.2.2","depth":2,"path":"Running-a-workflow.md","ref":"Running-a-workflow.md","articles":[]},{"title":"浏览器自动刷新","level":"1.2.3","depth":2,"path":"Automatic-browser-refresh.md","ref":"Automatic-browser-refresh.md","articles":[]},{"title":"引入文件","level":"1.2.4","depth":2,"path":"Requiring-files.md","ref":"Requiring-files.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-pro","fontsettings","livereload"],"root":"./content","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"search-pro":{},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{"version":"0.0.0"},"title":"test search plus plugin","gitbook":"*"},"file":{"path":"Getting-started.md","mtime":"2017-01-24T08:51:37.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-01-24T08:53:13.796Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    

    </body>
</html>

